library(stargazer)
library(ggplot2)
library(lfe)

load("bjps_panel.Rdata")

# Table 2 #

aggregate(weight_binary ~ pres_term + committee_dist_change, panel_all, mean)

# Table 3 #

mod1 <- felm(weight_binary ~  committee_dist_change | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod2 <- felm(weight_binary ~  committee_dist_change + total_polit + emp_diff + polit_diff | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod3 <- felm(weight_binary ~ scale(ideo_prox) + committee_dist_change | dyad + year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112 & panel_all$committee_dist_change!="Larger for Neither",])
mod4 <- felm(weight_binary ~ scale(ideo_prox)*committee_dist_change | dyad + year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112& panel_all$committee_dist_change!="Larger for Neither",])
mod5 <- felm(weight_binary ~ scale(ideo_prox) + committee_dist_change + avg_pres_dist + overlapping_laws + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad + year| 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod6 <- felm(weight_binary ~ scale(ideo_prox)*committee_dist_change + avg_pres_dist + overlapping_laws  + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])

stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "html",
          out = "congress.doc",
          keep.stat = c("n", "adj.rsq"))

# Table 4 #

mod1 <- felm(weight_binary ~  same_committee |  year | 0 | dyad, data=panel_all)
mod2 <- felm(weight_binary ~  same_committee + total_polit + emp_diff + polit_diff | year | 0 | dyad, data=panel_all)
mod3 <- felm(weight_binary ~ scale(ideo_prox) + same_committee | year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112 ,])
mod4 <- felm(weight_binary ~ scale(ideo_prox)*same_committee | year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112,])
mod5 <- felm(weight_binary ~ scale(ideo_prox) + same_committee + avg_pres_dist + overlapping_laws + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | year| 0 | dyad, data=panel_all)
mod6 <- felm(weight_binary ~ scale(ideo_prox)*same_committee + avg_pres_dist + overlapping_laws  + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | year | 0 | dyad, data=panel_all)

stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "html",
          out = "congress2.doc",
          keep.stat = c("n", "adj.rsq"))

# Table 5 #

aggregate(weight_binary ~ pres_term + committee_dist_change_maj, panel_all, mean)

# Table 6 #

mod1 <- felm(weight_binary ~  committee_dist_change_maj | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change_maj!="Larger for Neither",])
mod2 <- felm(weight_binary ~  committee_dist_change_maj + total_polit +  emp_diff + polit_diff | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change_maj!="Larger for Neither",])
mod3 <- felm(weight_binary ~ scale(ideo_prox) + committee_dist_change_maj | dyad + year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112 & panel_all$committee_dist_change_maj!="Larger for Neither",])
mod4 <- felm(weight_binary ~ scale(ideo_prox)*committee_dist_change_maj | dyad + year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112& panel_all$committee_dist_change_maj!="Larger for Neither",])
mod5 <- felm(weight_binary ~ scale(ideo_prox) + committee_dist_change_maj + avg_pres_dist + overlapping_laws + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad + year| 0 | dyad, data=panel_all[panel_all$committee_dist_change_maj!="Larger for Neither",])
mod6 <- felm(weight_binary ~ scale(ideo_prox)*committee_dist_change_maj + avg_pres_dist + overlapping_laws  + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change_maj!="Larger for Neither",])

stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "html",
          out = "congress3.doc",
          keep.stat = c("n", "adj.rsq"))


                       

panel_all$committee_dist_change_num <- case_when(
  panel_all$committee_dist_change == "Larger for Neither" ~ 0,
  panel_all$committee_dist_change == "Larger for One" ~ 1,
  TRUE ~ 2
)

variation <- aggregate(committee_dist_change_num ~ dyad,
                       panel_all, mean)
variation <- variation[!(variation$committee_dist_change_num %in% c(0,1,2)),]

# Figure 1 #

ggplot(panel_all, aes(x = congress, y = weight_binary, lty = same_committee))  + theme_minimal() + 
  stat_summary(geom = "line", fun = "mean") + theme(legend.position = "bottom") +
  scale_linetype(name = "") + 
  stat_summary(geom = "point", pch = 21, fill = "white", fun = "mean", size = 4) +
  ylab("") + xlab("Congress") + ggtitle("Proportion of Dyads Forming Coalitions") +
  scale_x_continuous(breaks = 104:115) + 
  scale_y_continuous(breaks = seq(0, .3, .05), labels = paste(seq(0, 30, 5), "%", sep = ""))

# Appendix B #

mod1 <- felm(weight_binary ~  committee_dist_change_continuous | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod2 <- felm(weight_binary ~  committee_dist_change_continuous + total_polit + emp_diff + polit_diff | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod3 <- felm(weight_binary ~ scale(ideo_prox) + committee_dist_change_continuous | dyad + year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112 & panel_all$committee_dist_change!="Larger for Neither",])
mod4 <- felm(weight_binary ~ scale(ideo_prox)*committee_dist_change_continuous | dyad + year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112& panel_all$committee_dist_change!="Larger for Neither",])
mod5 <- felm(weight_binary ~ scale(ideo_prox) + committee_dist_change_continuous + avg_pres_dist + overlapping_laws + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad + year| 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod6 <- felm(weight_binary ~ scale(ideo_prox)*committee_dist_change_continuous + avg_pres_dist + overlapping_laws  + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])

stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "html",
          out = "congress.doc",
          keep.stat = c("n", "adj.rsq"))

# Appendix C #

mod1 <- felm(weight_binary ~  committee_dist_change + year | dyad | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod2 <- felm(weight_binary ~  committee_dist_change + year + total_polit + emp_diff + polit_diff | dyad | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod3 <- felm(weight_binary ~ scale(ideo_prox) + committee_dist_change + year | dyad | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112 & panel_all$committee_dist_change!="Larger for Neither",])
mod4 <- felm(weight_binary ~ scale(ideo_prox)*committee_dist_change + year | dyad | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112& panel_all$committee_dist_change!="Larger for Neither",])
mod5 <- felm(weight_binary ~ scale(ideo_prox) + committee_dist_change + year + avg_pres_dist + overlapping_laws + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad| 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod6 <- felm(weight_binary ~ scale(ideo_prox)*committee_dist_change + year + avg_pres_dist + overlapping_laws  + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])

stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "html",
          out = "congress_appc.doc",
          keep.stat = c("n", "adj.rsq"))

mod1 <- felm(weight_binary ~  same_committee + year |  0 | 0 | dyad, data=panel_all)
mod2 <- felm(weight_binary ~  same_committee + year + total_polit + emp_diff + polit_diff | 0 | 0 | dyad, data=panel_all)
mod3 <- felm(weight_binary ~ scale(ideo_prox) + year + same_committee | 0 | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112 ,])
mod4 <- felm(weight_binary ~ scale(ideo_prox)*same_committee + year  | 0 | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112,])
mod5 <- felm(weight_binary ~ scale(ideo_prox) + same_committee + year + avg_pres_dist + overlapping_laws + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | 0| 0 | dyad, data=panel_all)
mod6 <- felm(weight_binary ~ scale(ideo_prox)*same_committee +year +  avg_pres_dist + overlapping_laws  + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | 0 | 0 | dyad, data=panel_all)

stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "html",
          out = "congress_appc.doc",
          keep.stat = c("n", "adj.rsq"))

# Appendix D # 

mod1 <- felm(ln_weight ~  committee_dist_change | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod2 <- felm(ln_weight ~  committee_dist_change + total_polit + emp_diff + polit_diff | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod3 <- felm(ln_weight ~ scale(ideo_prox) + committee_dist_change | dyad + year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112 & panel_all$committee_dist_change!="Larger for Neither",])
mod4 <- felm(ln_weight ~ scale(ideo_prox)*committee_dist_change | dyad + year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112& panel_all$committee_dist_change!="Larger for Neither",])
mod5 <- felm(ln_weight ~ scale(ideo_prox) + committee_dist_change + avg_pres_dist + overlapping_laws + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad + year| 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])
mod6 <- felm(ln_weight ~ scale(ideo_prox)*committee_dist_change + avg_pres_dist + overlapping_laws  + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | dyad + year | 0 | dyad, data=panel_all[panel_all$committee_dist_change!="Larger for Neither",])

stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "html",
          out = "congress_appd.doc",
          keep.stat = c("n", "adj.rsq"))



mod1 <- felm(ln_weight ~  same_committee |  year | 0 | dyad, data=panel_all)
mod2 <- felm(ln_weight ~  same_committee + total_polit + emp_diff + polit_diff | year | 0 | dyad, data=panel_all)
mod3 <- felm(ln_weight ~ scale(ideo_prox) + same_committee | year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112 ,])
mod4 <- felm(ln_weight ~ scale(ideo_prox)*same_committee | year | 0 | dyad, data=panel_all[panel_all$congress %in% 105:112,])
mod5 <- felm(ln_weight ~ scale(ideo_prox) + same_committee + avg_pres_dist + overlapping_laws + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | year| 0 | dyad, data=panel_all)
mod6 <- felm(ln_weight ~ scale(ideo_prox)*same_committee + avg_pres_dist + overlapping_laws  + total_polit +  log(total_mentions+1) + log(total_rules_sum) + emp_diff + polit_diff + avg_house_dist + avg_court_dist | year | 0 | dyad, data=panel_all)

stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "html",
          out = "congress_appd.doc",
          keep.stat = c("n", "adj.rsq"))
